<!--  活动列表组件  -->
<template>
  <div>
    <div class="">
      <el-dialog title="报名信息" :visible.sync="dialogFormVisible" :loading="loading" width="1200px">
        <el-button class="download" type="primary" icon="el-icon-download" size="mini" @click="exportExcel" />
        <el-table
          :loading="listLoading"
          :data="list.slice((this.currentPage-1)*pagesize,currentPage*pagesize)"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          class="applyTable"
        >
          <el-table-column
            align="center"
            prop="applyId"
            label="报名编号"
            width="100px"
          />

          <el-table-column
            align="center"
            prop="applyUserId"
            label="报名者学号"
            width="200px"
          />

          <el-table-column
            align="center"
            prop="applyUserName"
            label="报名者姓名"
            width="100px"
          />

          <el-table-column
            align="center"
            prop="applyUserClass"
            label="报名者班级"
            width="100px"
          />

          <el-table-column
            align="center"
            prop="applyUserPhone"
            label="报名者电话"
            width="120px"
          />

          <el-table-column
            align="center"
            prop="applyUserEmail"
            label="报名者邮箱"
            width="150px"
          />

          <el-table-column
            align="center"
            prop="applyStatus"
            label="报名状态"
            width="100px"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus=='0'">待审核</span>
              <span v-if="scope.row.applyStatus=='1'">已录取</span>
              <span v-if="scope.row.applyStatus=='-1'">已拒绝</span>

            </template>
          </el-table-column>

          <el-table-column align="center" label="操作">
            <template slot-scope="Scope">
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="admitApply(Scope.row.applyActivityId,Scope.row.applyUserId,Scope.row.applyStatus)">录取</el-button>
              <!-- <el-button type="warning" icon="el-icon-edit" @click="refuseApply(Scope.row.applyActivityId,Scope.row.applyUserId,Scope.row.applyStatus)" size="mini">拒绝</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <div id="pages">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10]"
            :page-size="pagesize"
            layout="total, sizes,prev, pager, next"
            :total="list.length"
            prev-text="上一页"
            next-text="下一页"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-dialog>
    </div>
  </div>

</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getApplyByActId } from '../../api/apply.js'
import { updateApply } from '../../api/apply.js'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  name: 'ApplyList',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    type: String,
    actId: String,
    show: {
      default: false,
      require: true
    }
  },
  data() {
    // 这里存放数据
    return {
      actId: '',
      show: false,
      dialogFormVisible: false,
      search: {},
      listLoading: false,
      list: [],
      currentPage: 1, // 默认显示页面为1
      pagesize: 10 //    每页的数据条数
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    /* getApplyByActId(this.actId).then((data)=>{
          this.list = data.obj
          console.log(data.obj)
          this.listLoading=false
      })*/
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    exportExcel() {
      var wb = XLSX.utils.table_to_book(document.querySelector('.applyTable'))
      console.log(document.querySelector('.applyTable'))
      var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
      try {
        FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '报名表.xlsx')
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    },
    // 每页下拉显示数据
    handleSizeChange(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange(currentPage) {
      console.log(this)
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    admitApply(actId, applyUserId, applyStatus) {
      if (applyStatus == '0') {
        updateApply(actId, applyUserId).then((data) => {
          this.$message({
            showClose: true,
            message: '录取成功',
            type: 'success'
          })
          location.reload()
        })
      } else if (applyStatus == '1') {
        this.$message({
          showClose: true,
          message: '该学生已录取',
          type: 'warning'
        })
      } else {
        this.$message({
          showClose: true,
          message: '该学生已拒绝',
          type: 'warning'
        })
      }
    },
    showList(actId) {
      console.log(actId)
      this.title = '查看活动信息'
      getApplyByActId(actId).then((data) => {
        this.list = data.obj
        console.log(data.obj)
        this.listLoading = false
        this.dialogFormVisible = true
      })
    }

  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
.download{
  float: right;
  margin-bottom: 15px;
  margin-right: 40px;
}
</style>
